<?php
namespace frame\libs;

use frame\libs\Config;
use frame\libs\JsonResp;
use frame\libs\Session;
use frame\libs\Log;
use app\admin\model\UserModel;
use app\admin\model\NodeModel;

/**
 * 基础控制器
 */
abstract class BaseController
{
    /**
     * rbac检查
     * @return [type] 1通过 2未登录 3权限不足
     */
    protected function check($action = null)
    { 
        // 登录验证开关开启
        if (!Config::get('login_on', 'authorize')) {
            return 1;
        }
        // 权限开关开启
        if (!Config::get('rbac_on', 'authorize')) {
            return 1;
        }
        // 检查会话
        $uid = Session::get('uid');
        if (empty($uid)) {
            return 2;
        }
        if (!is_numeric($uid)) {
            return 2;
        }
        // 用户是否存在
        $user = UserModel::getInstance()->getUserWithRoleByUid($uid);
        if (!$user) {
            return 2;
        }
        $this->log();
        //检查用户状态
        if (!$user['status']) {
            return 3;
        }
        //检查用户认证方式 1实时认证 2登录认证
        if ($user['auth_type'] == 2) {
            return 1;
        }
        //检查角色状态
        if (!$user['r_status']) {
            return 3;
        }
        //获取角色所有权限节点id数组
        $nodes = $user['nodes'];
        if ($nodes && strpos($nodes, ',') !== false) {
            $nodes = explode(',', $nodes);
        } else {
            $nodes = [$nodes];
        }
        $a = $action ? $action : MODULE."/".CONTROLLER."/".ACTION;
        $node = NodeModel::getInstance()->getNodeByAction($a);
        if (!$node) {
            return 3;
        }
        if (!$node['status']) {
            return 3;
        }
        if (!in_array($node['id'], $nodes)) {
            return 3;
        }
        return 1;
    }

    /**
     * ajax验证
     * @return [type] [description]
     */
    public function checkAjax()
    {
        $status = $this->check();
        if ($status == 1) {
            return true;
        }elseif ($status == 2) {
            echo $this->unauthorized();
            return false;
        }elseif ($status == 3) {
            echo $this->forbidden();
            return false;
        }
        echo $this->error();
        return false;
    }

    /**
     * 页面直接访问验证
     * @return [type] [description]
     */
    protected function checkPage()
    {
        $status = $this->check();
        if ($status == 1) {
            return true;
        }elseif ($status == 2) {
            $this->alert('请登录！', 'index.php?m=admin&c=Login&a=index');
            return false;
        }elseif ($status == 3) {
            $this->jump('', 'index.php?m=index&c=Forbidden&a=index');
            return false;
        }
    }

    /**
     * a标签跳转前ajax验证
     * @return [type] [description]
     */
    public function checkBeforeJump()
    { 
        if (!isset($_POST['module'])) {
            echo $this->error(null, '非法请求1！');
            return false;
        }
        if (!isset($_POST['controller'])) {
            echo $this->error(null, '非法请求2！');
            return false;
        }
        if (!isset($_POST['action'])) {
            echo $this->error(null, '非法请求3！');
            return false;
        }
        $m = $_POST['module'];
        $c = $_POST['controller'];
        $a = $_POST['action'];
        $action = $m.'/'.$c.'/'.$a;
        $status = $this->check($action);
        if ($status == 1) {
            echo $this->success();
            return true;
        }
        if ($status == 2) {
            echo $this->unauthorized();
            return false;
        }
        if ($status == 3) {
            echo $this->forbidden();
            return false;
        }
    }

    /**
     * 页面跳转
     * @param  [type]  $message [description]
     * @param  string  $url     [description]
     * @param  integer $time    [description]
     * @return [type]           [description]
     */
    protected function jump($message='', $url='?', $time=0)
    {
        echo "<h2>{$message}</h2>";
        header("refresh:{$time};url={$url}");
        die();
    }

    /**
     * 页面弹窗后跳转
     * @param  [type] $message [description]
     * @param  string $url     [description]
     * @return [type]          [description]
     */
    protected function alert($message, $url='?')
    {
        echo "<script>alert('{$message}');window.location.href='{$url}';</script>";
        die();
    }

    /**
     * 日志保存
     * @param  string $username [description]
     * @return [type]           [description]
     */
    public function log()
    {
        Log::init()->save();
    }

    /**
     * 统一返回接口
     * @param  [type]  $data     [description]
     * @param  string  $msg      [description]
     * @param  integer $code     [description]
     * @param  boolean $transfer [description]
     * @return [type]            [description]
     */
    protected function success($data=null, $msg="success", $code=200, $transfer = false)
    {
        return JsonResp::success($data, $msg, $code, $transfer);
    }

    /**
     * 统一返回接口
     * @param  [type]  $data     [description]
     * @param  string  $msg      [description]
     * @param  integer $code     [description]
     * @param  boolean $transfer [description]
     * @return [type]            [description]
     */
    protected function error($data=null, $msg="error", $code=500, $transfer = false)
    {
        return JsonResp::error($data, $msg, $code, $transfer);
    }

    /**
     * 统一返回接口
     * @param  [type]  $data     [description]
     * @param  string  $msg      [description]
     * @param  integer $code     [description]
     * @param  boolean $transfer [description]
     * @return [type]            [description]
     */
    protected function json($data=null, $msg="", $code=0, $transfer = false)
    {
        return JsonResp::json($data, $msg, $code, $transfer);
    }

    /**
     * 统一返回接口
     * @param  [type]  $data     [description]
     * @param  string  $msg      [description]
     * @param  integer $code     [description]
     * @param  boolean $transfer [description]
     * @return [type]            [description]
     */
    protected function forbidden($data=null, $msg="权限不足！", $code=403, $transfer = false)
    {
        return JsonResp::forbidden($data, $msg, $code, $transfer);
    }

    /**
     * 统一返回接口
     * @param  [type]  $data     [description]
     * @param  string  $msg      [description]
     * @param  integer $code     [description]
     * @param  boolean $transfer [description]
     * @return [type]            [description]
     */
    protected function unauthorized($data=null, $msg="未登录！", $code=403, $transfer = false)
    {
        return JsonResp::unauthorized($data, $msg, $code, $transfer);
    }

}